package com.briup.mt.service.impl;

import com.briup.mt.bean.Application;
import com.briup.mt.bean.ApplicationExample;
import com.briup.mt.dao.ApplicationMapper;
import com.briup.mt.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ApplicationServiceimpl implements ApplicationService {

    @Resource
    private ApplicationMapper applicationMapper;

    @Override
    public List<Application> find(String name) {
        // 模板
        ApplicationExample example = new ApplicationExample();
        if(!StringUtils.isEmpty(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        List<Application> list = applicationMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Application application) {
        // 如果userInfo中存在id属性，表示更新；否则保存
        if(application.getId() == null){
            application.setCreateTime(new Date().getTime());
            applicationMapper.insert(application);
        } else {
            applicationMapper.updateByPrimaryKey(application);
        }
    }

    @Override
    public void deleteById(long id) {
        applicationMapper.deleteByPrimaryKey(id);
    }
}
